
$(function(){
	
	myselfChoose(1);
});

//选择条
function myselfChoose(i){
	if(i==1){
		//改变导航栏样式
		$("#oneLi").addClass("active");
		$("#twoLi").removeClass("active");       			
		//改变所展示的课程信息的显示情况
		$("#oneContainer").css("display","block");
		$("#twoContainer").css("display","none");  
		
		//向后台请求所有教练信息
		$.post("/getAllCoach",function(data){
			showAllCoachInfo(data);
		});
		showAllCoachInfo(data);	
	}
	if(i==2){
		$("#oneLi").removeClass("active");
		$("#twoLi").addClass("active");       		
		$("#oneContainer").css("display","none");
		$("#twoContainer").css("display","block");
	}
}    
data=[{
	"cid":1,
	"mobile":"13588892170",
	"sample":"trainer1.png"
},
{
	"cid":2,
	"mobile":"13588892171",
	"sample":"trainer3.png"
}];

$("#deleteCoach").click(function () {
	if(confirm("如果该教练还存在在职课程则无法删除")){
        $.post("/deleteCoach",{"cid":$("#updateCid").val()},function (data) {
            if(data==1){
                promptSuccess();
                myselfChoose(1);
            }else{
                promptfailure();
            }
        });
	}
});

//添加教练
$("#addCoach").click(function(){
 	var coach = new Object()
 	coach.cname = $("#cname").val();
 	coach.sample = $("#fileName").val();
	coach.mobile = $("#mobile").val();
	coach.mold = $("#mold").val();
	
	var testCname = /^[\u4e00-\u9fa5]{0,}$/;
	var testMobile = /^1[3,5,8]\d{9}$/;
	if(!testCname.test(coach.cname)||coach.cname==''){
		promptfailure();
		$("#informationDiv").slideDown(500);
		$("#information").val("教练名称必须是汉字");
		return false;
	}
	if(!testMobile.test(coach.mobile)||coach.mobile==''){
		promptfailure();
		$("#informationDiv").slideDown(500);
		$("#information").val("手机号需为13、15、18开头11位数字");
		return false;
	}
	$.post("/addCoach",coach,function(data){
		if(data!='error'){
			promptSuccess();
			$("#informationDiv").css("display","none");
			$("#loginInfoDiv").css("display","block");
			$("#username").val(coach.cname);
			$("#password").val(coach.mobile);
		}else{
			promptfailure();
			$("#loginInfoDiv").css("display","none");
			$("#informationDiv").css("display","block");
			$("#information").val("该手机号已被注册");			
		}
	});
});

//将所有教练信息进行前端渲染
function showAllCoachInfo(data){
	var coachDiv = "";
	for(var i=0;i<data.length;i++){
	  coachDiv = coachDiv+
		"<div class='col-sm-3 col-xs-12'>"+
	       "<div class='trainers-box'>"+
	            "<figure>"+
	            	"<img src='images/"+data[i].sample+"' width='262.5px' height='262.5px'>"+
	            "</figure>"+                             
	           	"<a href='' onclick='showCoachInfoByCid("+data[i].cid+");' data-toggle='modal' data-target='#coachInfo'><span>教练详情</span></a>"+
	           	"<a href='' onclick='showHonorByCid("+data[i].cid+");' data-toggle='modal' data-target='#honorInfo'><span>教练荣誉</span></a>"+
	           	"<a href='' onclick='showLoginInfoByMobile("+data[i].mobile+");' data-toggle='modal' data-target='#loginInfo'><span>登录信息</span></a>"+	            
	        "</div>"+
	    "</div> ";
	}	
	$("#coachDiv").html(coachDiv);
}

//获取教练详情
function showCoachInfoByCid(cid){	
	$.post("/getOneCoachInfo",{"cid":cid},function(data){
		$("#updateCid").val(data.cid);
		$("#updateName").val(data.cname);
		$("#updateMold").val(data.mold);
		$("#updateMobile").val(data.mobile);
		$("#updateRank").val(data.rank);
	});
}

$("#updateCoach").click(function(){
	var coach = new Object();
	coach.cid = $("#updateCid").val();
	coach.mold = $("#updateMold").val();
	$.post("/updateCoachMold",coach,function(){
		promptSuccess();
	});
});

//生成10000-60000的随机整数
function randomData() {
        return Math.round(Math.random() * 50000+10000);
}

data2 = [{
	"id":1,
	"info":"世界冠军"
},{
	"id":2,
	"info":"世界冠军"
},{
	"id":3,
	"info":"世界冠军"
}]

//获取教练荣誉
function showHonorByCid(cid){
	$("#honorCid").val(cid);
	console.log(cid);
	$.post("/getHonorsByCid",{"cid":$("#honorCid").val()},function(data2){
		showdata2(data2);
	});
	
}

function showdata2(data2){
	var showHonor = "";
	for(var i = 0; i<data2.length;i++){
		showHonor = showHonor+
	    "<div class='input-group  input-group-lg' id='div"+data2[i].id+"'>"+
		"<span class='input-group-addon'><a href='javascript:;' onclick='deleteHonorByDB("+data2[i].id+")'>删除</a></span>"+ 
		"<input  class='form-control' value='"+data2[i].info+"' id='input"+data2[i].id+"'>"+
	 	"<span class='input-group-addon'><a href='javascript:;' onclick='saveHonor("+data2[i].id+")'>保存</a></span></div>";
	}
	$("#honors").html(showHonor);
}

function deleteHonorByDB(id){
    //请求后台删除
    $.post("/deleteHonor",{"id":id},function(){
    	//并显示提示信息
	    promptSuccess();
	    //并将该div移除
	    $("#div"+id).remove();
    });
   
}

$("#addHonor").click(function(){
	var addBefore = $("#honors").html();
	var randSum = randomData();
	console.log(randSum);
	var addOne = 
		"<div class='input-group  input-group-lg' id='div"+randSum+"'>"+
		 "<span class='input-group-addon'><a href='javascript:;' onclick='deleteHonor("+randSum+")'>删除</a></span>"+ 
		 "<input  class='form-control' id='input"+randSum+"'>"+
	 	"<span class='input-group-addon'><a href='javascript:;' onclick='saveHonor("+randSum+")'>保存</a></span></div>";
	 $("#honors").html(addBefore+addOne);
});

function deleteHonor(id){
	promptSuccess();
	$("#div"+id).remove();	
}

function saveHonor(id){
	var cid = $("#honorCid").val();//用于重新加载模态框内容
	var honor = new Object();
	honor.id = id;
	honor.cid = cid;
	honor.info =  $("#input"+id).val().replace(/(^\s*)|(\s*$)/g, '');
	if(honor.info==''){
		promptfailure();
		$("#input"+id).attr("placeholder","不符合要求");
		return false;
	}
	promptSuccess();
	console.log(honor);
	
	//请求后台更新或添加addOrUpdate
    $.post("/addOrUpdateHonor",honor,function(){
    	//显示提示信息
		promptSuccess();

        //重新加载该模态框数据
        $.post("/getHonorsByCid",{"cid":cid},function(data2){
            showdata2(data2);
        });
    });	

}

//获取教练登录信息
function showLoginInfoByMobile(mobile){
	$.post("/getCoachLoginInfoByMobile",{"mobile":mobile},function(data){
		$("#updateUid").val(data.uid);
		$("#updateUserName").val(data.username);
		$("#updatePassword").val(data.password);
	});	
}

$("#updateLoginInfo").click(function(){
 	var user = new Object();
 	user.uid = $("#updateUid").val();
 	user.password = $("#updatePassword").val().replace(/(^\s*)|(\s*$)/g, ''); 
	console.log(user);
	if(user.password==''){
		promptfailure();
		return false;
	}
	$.post("/changeLoginInfoPwd",user,function(){
		promptSuccess();
	});
});

 //实现成功提示框，且自动消失
    function promptSuccess(){   	
    	 $("#info").css("display","block");
    	 $("#msg").html("<p style='font-size:50px;padding-top:30px;'>成功</p>");
    	 $("#sign").html("√");
    	 window.setTimeout(function(){
    	 	$("#info").css("display","none");
    	 },1000);
    }
    
    //实现失败提示框，且自动消失
    function promptfailure(){
    	$("#info").css("display","block");
    	 $("#msg").html("<p style='font-size:50px;padding-top:30px;'>失败</p>");
    	 $("#sign").html("×");
    	 window.setTimeout(function(){
    	 	$("#info").css("display","none");
    	 },1000);
    }
   //上传图片
	$("#pic").click(function () {
			$("#upload").click(); //隐藏了input:file样式后，点击头像就可以本地上传
			$("#upload").on("change",function(){
				 var formData = new FormData();
         formData.append('file', $('#upload')[0].files[0]);
         formData.append("selectId", '0');
         $.ajax({
             url: "/courseUpload",
             type: "POST",
             data: formData,
             contentType: false,             
             processData: false,
             success: function (data) {
				$("#fileName").val(data);
             },
             error: function () {
                 alert("上传失败！");

             }
         });							
		var objUrl = getObjectURL(this.files[0]) ; //获取图片的路径，该路径不是图片在本地的路径
		if (objUrl) {
				$("#pic").attr("src", objUrl) ; //将图片路径存入src中，显示出图片
		}
		});
	});
	
	//取消图片
    function stopUpload(){ 	
    	$("#pic").attr("src","images/trainer10.png");
    	$("#fileName").val("trainer10.png");
    }
	    //建立一個可存取到該file的url
	function getObjectURL(file) {	
		var url = null ;
		if (window.createObjectURL!=undefined) { // basic
			url = window.createObjectURL(file) ;
		} else if (window.URL!=undefined) { // mozilla(firefox)
			url = window.URL.createObjectURL(file) ;
		} else if (window.webkitURL!=undefined) { // webkit or chrome
			url = window.webkitURL.createObjectURL(file) ;
		}
		return url ;
	}				
    
